C Interview Questions and Answers
Question: How do you write a function that can reverse a linked-list?
Answer: void reverselist(void) { if(head==0) return; if(head->next==0) return; if(head->next==tail) { head->next = 0; tail->next = head; } else { node* pre = head; node* cur = head->next; node* curnext = cur->next; head->next = 0; cur-> next = head; for(; curnext!=0; ) { cur->next = pre; pre = cur; cur = curnext; curnext = curnext->next; } curnext->next = cur; } } |
Is it helpful?
Yes
No
Most helpful rated by users:
- What is C++?
- What is function overloading and operator overloading?
- How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
- What is the difference between declaration and definition?
- What is the difference between realloc() and free()?